#pragma once
#include "listselectordlg.h"
#include "SkinnedButton.h"
#include <map>

class CPickFileItem : public CSelectorListItem
{
public:
  CPickFileItem(const TCHAR* name, const TCHAR* fullName, DWORD attribs);

  // Drawing
  virtual void Draw(HDC hdc, const RECT& rc) const;

  // Commands
  virtual bool DoSelect(HWND hParent)
  { SendCommand(hParent, IDC_PICKFILE_SELECT); return true; }

  // DATA
  const CString& GetFullName() const
  { return m_fullName; }
  DWORD GetAttribs() const
  { return m_attribs; }

protected:
  CString m_fullName;
  CIcon   m_icon;
  DWORD   m_attribs;
};

class CPickFileFolderItem : public CPickFileItem
{
public:
  CPickFileFolderItem(const TCHAR* name, const TCHAR* fullName, DWORD attribs, bool canSelect = true)
    : CPickFileItem(name, fullName, attribs), m_canSelect(canSelect)
  {}

  // Interface
  virtual bool OnMouseClick(HWND hParent, int x, int y)
  { return DoOpen(hParent); }
  virtual bool OnKeyUp(HWND hParent, int vKey)
  { if (vKey == VK_TACTION) return DoOpen(hParent); else return false; }
  virtual bool OnLongTap(HWND hParent)
  { return DoSelect(hParent); }
  virtual bool IsLongTapValid(HWND hParent) { return m_canSelect; }

  // Commands
  virtual bool DoOpen(HWND hParent)
  { SendCommand(hParent, IDC_PICKFILE_FOLDER); return true; }


protected:
  bool m_canSelect;
};

class CPickFileUpItem : public CSelectorListItem
{
public:
  CPickFileUpItem()
    : CSelectorListItem(LANG("folderUp"))
  { m_image = CTheme::GetImage(timFolderUp); }

  // Drawing
  virtual void Draw(HDC hdc, const RECT& rc) const;

  // Commands
  virtual bool DoSelect(HWND hParent)
  { SendCommand(hParent, IDC_PICKFILE_UP); return true; }

protected:
  CImage m_image;
};

class CPickFileData
{
public:
  CString initialFolder;
  CStringVector allowedExts;
  DWORD   attrExclude;
  DWORD   attrRequired;
  bool    showExts;

  CPickFileData(const TCHAR* sInitFolder = TEXT(".\\"),
    DWORD dwAttrExclude = FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN,
    DWORD dwAttrRequired = -1, bool bShowExts = true) : initialFolder(sInitFolder),
    attrExclude(dwAttrExclude), attrRequired(dwAttrRequired), showExts(bShowExts)
  {}
};

class CPickFolderData : public CPickFileData
{
public:
  CPickFolderData(const TCHAR* sInitFolder = TEXT(".\\"),
    DWORD dwAttrExclude = FILE_ATTRIBUTE_HIDDEN,
    DWORD dwAttrRequired = FILE_ATTRIBUTE_DIRECTORY, bool bShowExts = true)
    : CPickFileData(sInitFolder, dwAttrExclude, dwAttrRequired, bShowExts)
  {}
};

class CPickFileDlg : public CListSelectorDlg
{
  typedef std::map<CString, DWORD> CFindFileMap;

public:
  CPickFileDlg(const CPickFileData& pickData, int listItemHeight = CTheme::iSelectorListItemSize)
    : CListSelectorDlg(listItemHeight), m_pickData(pickData)
  {}

	BEGIN_MSG_MAP(CPickFileDlg)
    MESSAGE_HANDLER(WM_CREATE, OnCreate)
    MESSAGE_HANDLER(WM_SIZE, OnSize)

    COMMAND_ID_HANDLER_EX(IDC_PICKFILE_SELECT, OnFileSelect)
    COMMAND_ID_HANDLER_EX(IDC_PICKFILE_FOLDER, OnFolderOpen)
    COMMAND_ID_HANDLER_EX(IDC_PICKFILE_UP, OnFolderUp)
    COMMAND_ID_HANDLER_EX(IDC_PICKFILE_ROOT, OnFolderRoot)

    CHAIN_MSG_MAP(CListSelectorDlg)
	END_MSG_MAP()

  LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  LRESULT OnSize(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
  
  void UpdateSize();

  void OnFileSelect(UINT uNotifyCode, int nID, CWindow wndCtl);
  void OnFolderOpen(UINT uNotifyCode, int nID, CWindow wndCtl);
  void OnFolderUp(UINT uNotifyCode, int nID, CWindow wndCtl);
  void OnFolderRoot(UINT uNotifyCode, int nID, CWindow wndCtl);

  virtual void SetSelection(const CString& file);

protected:
  void FillList();

protected:
  CPickFileData m_pickData;
  CString m_folder;
  CSkinnedButton m_btnUp;
  CSkinnedButton m_btnRoot;
};
